package com.game.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.common.Result;
import com.game.sys.entity.User;
import com.game.sys.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author limeng
 * @since 2023-03-23
 */
@Api(tags = {"用户接口列表"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("/all")
    public Result<List<User>> getAll(){
        List<User> list = userService.list();
        return Result.success(list,"查询成功");
    }

    @PutMapping
    public Result<?> updateUser(@RequestBody User user){
        user.setPassword(null);
        userService.updateUser(user);
        return Result.success("修改用户成功");
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteUserById(@PathVariable Integer id){
        userService.deleteUserById(id);
        return Result.success("删除用户成功");
    }

    @PostMapping
    public Result<?> addUser(@RequestBody User user){
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userService.addUser(user);
        return Result.success("添加用户成功");
    }

    @GetMapping("/list")
    public Result<Map<String,Object>> getUserList
            (@RequestParam(value = "name",required = false) String name,
             @RequestParam(value = "phone",required = false) String phone,
             @RequestParam("pageNo") Long pageNo,
             @RequestParam("pageSize") Long pageSize){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(name),User::getName,name);
        wrapper.like(StringUtils.hasLength(phone),User::getPhone,phone);
        Page<User> page = new Page<>(pageNo,pageSize);
        userService.page(page,wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getRecords());
        return Result.success(map);
    }

    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable("id") Integer id){
        User byId = userService.getUserById(id);
        return Result.success(byId);
    }

    @PostMapping("/login")
    public Result<Map<String,Object>> login(@RequestBody User user){
        Map<String,Object> data = userService.login(user);
        if(data != null){
            return Result.success(data);
        }
        return Result.fail(20002,"用户名或密码错误");
    }

    @GetMapping("/info")
    public Result<Map<String,Object>> getUserInfo(@RequestParam("token") String token){
        Map<String, Object> map = userService.getUserInfo(token);
        if (map != null){
            return Result.success(map);
        }
        return Result.fail("信息有误");
    }

    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("token") String token){
        userService.logout(token);
        return Result.success();
    }
}
